package com.survey.wxapp.controller;

import com.survey.common.annotation.Anonymous;
import com.survey.common.core.controller.BaseController;
import com.survey.common.core.domain.AjaxResult;
import com.survey.common.core.redis.RedisCache;
import com.survey.wxapp.domain.WeChatPhoneRequest;
import com.survey.wxapp.service.WeChatDecryptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Anonymous
@RestController
@RequestMapping("/wx/getPhone")
public class WeChatController extends BaseController {

    @Autowired
    private WeChatDecryptService weChatDecryptService;
    @Autowired
    private RedisCache redisCache;

    @PostMapping("/DecryptPhoneNumber")
    public AjaxResult decryptPhoneNumber(@RequestBody WeChatPhoneRequest request) {
        try {
            String phoneNumber = weChatDecryptService.getPhoneNumber(request.getEncryptedData(),
                    request.getIv(),
                    redisCache.getCacheObject("session_key"));
            return success(phoneNumber);
        } catch (Exception e) {
            e.printStackTrace();
            return error("获取失败");
        }
    }
}
